<?php
declare (strict_types=1);

namespace app\service;

class CurlHandleService extends \think\Service
{
    function fetch()
    {
        $url = 'https://wm.xuanqingkj.cn/jrNJyBfHxQ.php/user/user/index';
        $params = [
            'sort' => 'id',
            'order' => 'asc',
            'offset' => 0,
            'limit' => 1,
            '_' => time() . rand(100, 999)
        ];
        $url = $url . '?' . http_build_query($params);
//var_dump($url);exit;

        $curl = curl_init();

// 设置要抓取的URL

        curl_setopt($curl, CURLOPT_URL, $url);

// 设置自定义的请求头
        $headers = array(
            'Accept:application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding:gzip, deflate, br',
            'Accept-Language:zh-CN,zh;q=0.9',
            'Content-Type:application/json',
            'Cookie:sidebar_collapse=0; PHPSESSID=v8nasg825vvqm3buqma4sn1eml; think_var=zh-cn',
            'Sec-Ch-Ua:"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"',
            'Sec-Ch-Ua-Mobile:?0',
            'Sec-Ch-Ua-Platform:"Windows"',
            'Sec-Fetch-Dest:empty',
            'Sec-Fetch-Mode:cors',
            'Sec-Fetch-Site:same-origin',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
            'X-Requested-With:XMLHttpRequest',
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 设置自定义的请求头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 将抓取的内容以字符串返回而不是直接输出
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
        curl_setopt($curl, CURLOPT_TIMEOUT, 10); // 设置超时时间为10秒

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);


// 其他设置和执行请求的代码...
        $response = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error: ' . curl_error($curl);
        } else {
            echo $response;
//    $response_array = json_decode($response, true);
//    var_dump($response_array);
        }
        curl_close($curl);

    }
}
